package me.idashu.attg.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import me.idashu.attg.domain.AdminEntity;
import me.idashu.attg.service.AdminService;
import me.idashu.attg.util.WeekDay;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private AdminService adminService;
	
	@RequestMapping(value = "/web/left")
	public String preLeft(Locale locale, Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		AdminEntity admin= (AdminEntity) session.getAttribute("user");
		model.addAttribute("user", admin);
		return "left";
	}

	@RequestMapping(value = "/web/top")
	public String pretop(Locale locale, Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		AdminEntity admin= (AdminEntity) session.getAttribute("user");
		model.addAttribute("user", admin);

		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(date);
		String weekday = WeekDay.getWeekOfDate(date);
		System.out.println(weekday);
		model.addAttribute("today", today);
		model.addAttribute("weekday", weekday);
		model.addAttribute("adminEntity", admin);
		if ("shop".equals(admin.getRole())) {
			model.addAttribute("role", "商户");
		} else if ("admin".equals(admin.getRole())) {
			model.addAttribute("role", "管理员");
		} else if ("supAdmin".equals(admin.getRole())) {
			model.addAttribute("role", "超级管理员");
		}
		return "top"; 
	}

	@RequestMapping(value = "/web/login")
	public String preLogin(Locale locale, Model model) {
		return "user/login";
	}

	/**
	 * 
	 * @param locale
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/web/tologin",method=RequestMethod.POST)
	public String toLogin(@ModelAttribute AdminEntity adminEntity,
			HttpServletRequest request) {
			AdminEntity admin = adminService.findAdminByNameAndPass(adminEntity);
			if (admin!= null) {
				HttpSession sessions = request.getSession();
				sessions.setAttribute("user", admin);
			}else{
				return "user/login";
			}
			System.out.println(admin);
			return "index";
	}


	@RequestMapping(value = "/web/left", method = RequestMethod.GET)
	public String left(Locale locale, Model model) {

		return "left";
	}

	@RequestMapping(value = "/web/breadcrumb", method = RequestMethod.GET)
	public String breadcrumb(Locale locale, Model model) {

		return "breadcrumb";
	}

	@RequestMapping(value = "/web/main", method = RequestMethod.GET)
	public String mains(Locale locale, Model model) {

		return "main";
	}
	
	@RequestMapping(value = "/checkLogin")
	public void preCheckLoginName(@ModelAttribute AdminEntity adminEntity,
			HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html;charset=GBK");
		try {
			String checkResult = null;
			AdminEntity admin = adminService.findAdminByNameAndPass(adminEntity);
			if (admin != null) {
				checkResult = "0";
			} else {
				checkResult = "1";
			}
			response.getWriter().print(checkResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/exitSystem")
	public void exitSystem(Model model,HttpServletResponse response,HttpServletRequest request){
		response.setContentType("text/html;charset=GBK");
		try {
			String checkResult ="0";
			HttpSession session = request.getSession();
			session.removeAttribute("user");
			response.getWriter().print(checkResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
